//
//  TrimmedProperty.swift
//  EasyCoding
//
//  Created by JY_NEW on 2020/6/4.
//

import Foundation

///自动去除字条串首尾的空字符或指定字符
@propertyWrapper
public struct PWTrimmed {
    private(set) var value: String = ""
    private let characterSet: CharacterSet
    
    public var wrappedValue: String {
        get { value }
        set { value = newValue.trimmingCharacters(in: characterSet) }
    }
    
    public init(wrappedValue: String = "", chars: CharacterSet = .whitespacesAndNewlines) {
        self.characterSet = chars
        self.wrappedValue = wrappedValue
    }
}

///该标记的字符串比较时不区分大小写
@propertyWrapper
public struct PWCaseInsensitive<Value: StringProtocol> {
    public var wrappedValue: Value
    public init(wrappedValue: Value) {
        self.wrappedValue = wrappedValue
    }
}
extension PWCaseInsensitive: Comparable {
    private func compare(_ other: PWCaseInsensitive<Value>) -> ComparisonResult {
        wrappedValue.caseInsensitiveCompare(other.wrappedValue)
    }

    public static func == (lhs: PWCaseInsensitive<Value>, rhs: PWCaseInsensitive<Value>) -> Bool {
        lhs.compare(rhs) == .orderedSame
    }

    public static func < (lhs: PWCaseInsensitive<Value>, rhs: PWCaseInsensitive<Value>) -> Bool {
        lhs.compare(rhs) == .orderedAscending
    }

    public static func > (lhs: PWCaseInsensitive<Value>, rhs: PWCaseInsensitive<Value>) -> Bool {
        lhs.compare(rhs) == .orderedDescending
    }
}
